import waves from "@/directive/waves"; // Waves directive
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import { alertMessage, alertWithCallback } from "@/utils/utils";
import { mapGetters } from 'vuex';
import selectDevice from "@/business-components/select-device/select-device";
import carDetail from "@/business-components/car-detail/car-detail";
import playTrack from "@/business-components/play-track/play-track";
import {
  getCompanyItems,
  getAlertData,
  releaseAlertApi
} from "@/views/car-manage/car.service";

export default {
  components: { Pagination, selectDevice, carDetail, playTrack },
  directives: { waves },
  data() {
    return {
      file_url: process.env.VUE_APP_FILE_URL, // 头像url
      recordNum: 20, // 分页组件默认的页数, 分页里的select默认10, 20, 50
      totalNum: 0, // 数据总数
      pageNum: 1, // 页数
      data: [], // 用户列表
      searchCriteria: {
        queryStr: this.$route.query.carPlate ? this.$route.query.carPlate : ''
      }, // 搜索条件
      showSelectCarDetailDialog: false, // 车辆详情
      showPlayTrackDialog: false, // 轨迹回放弹窗
      selectedTrack: [], // 轨迹回放数据
      companies: [], // 运输单位
      vehicleId: 0,
      equipId: 0
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    this.getCompanyList();
    this.getDataList();
  },
  methods: {
    // 获取列表
    getDataList() {
      this.searchCriteria.pageNum = this.pageNum;
      this.searchCriteria.recordNum = this.recordNum;
      getAlertData(this.searchCriteria).then(res => {
        this.totalNum = res.dataCount;
        this.data = res.data;
      });
    },

    // 根据关键字进行搜索
    search() {
      this.pageNum = 1;
      this.getDataList();
    },

    // 重置搜索
    reset() {
      this.pageNum = 1;
      this.searchCriteria = {};
      this.getDataList();
    },

    // 运输单位列表
    getCompanyList() {
      getCompanyItems().then(res => {
        this.companies = res.data;
      });
    },

    // 解除报警
    releaseAlert(id) {
      alertWithCallback('确认解除警报吗?', '').then(yes => {
        if (yes) {
          releaseAlertApi(id).then(res => {
            this.getDataList();
            alertMessage(res.message)
          })
        }
      }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 显示车辆详情
    showCarDetailDialog(rowData) {
      this.showSelectCarDetailDialog = true;
      this.equipId = rowData.objectId;
      this.vehicleId = rowData.vehicleId;
    },

    // 轨迹播放
    goMachinePlayback(trackList) {
      this.showPlayTrackDialog = true;
      this.selectedTrack = trackList;
    }
  }
}
